﻿/* $RCSFile: CalloutCampaign.ascx.cs $
 * Copyright (C) Addon Nice Technologies 2010
 * $Source: ?/Addon Nice/AddonNice/AddonNice.LeadProspection/PageModules/CalloutCampaign/CalloutCampaign.ascx.cs $
 * $log$
 * Revision 24 2010/08/09 11:16:26  christian.surieux
 *   Added Log comment to all .ascx .aspx .master files
 * Revision 11 2010/08/09 10:18:24  christian.surieux
 *   Added Log comment to all cs files
 */

using System;
using System.Globalization;
using System.Diagnostics;
using System.IO;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Web;
using System.Web.Configuration;
using AddonNice.Configuration.Settings;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using System.Web.Security;
using System.Xml;
using System.Configuration;

using AddonNice;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Security;
using AddonNice.UI.DataTypes;
using AddonNice.Helpers;
using AddonNice.Diagnostics;
using AddonNice.Settings;
using AddonNice.Core;

using AddonNice.CRMWrapper;
using AddonNice.CRMWrapper.Design;

using AddonNice.LeadProspection.Settings;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.CalloutCampaign
{

    /// <summary>
    /// Initializer class for CalloutCampaign 
    /// </summary>
    public class CalloutCampaignSI : PortalCRMModuleControlSI
    {
        public CalloutCampaignSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]CalloutCampaignSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]CalloutCampaignSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]CalloutCampaignSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);

            string extgrp                       =   pm.ExtendedResourcesGroup;
            SettingItemSpecial CacheDelay       =   new SettingItemSpecial(PStr.CacheDelay,new IntegerDataType());
            CacheDelay.Required                 =   false;
            CacheDelay.ResourceFile             =   PStr.ResourceFile;
            CacheDelay.ResourceItem             =   PStr.ResourceItem;
            CacheDelay.Value                    =   "30";
            CacheDelay.Order                    =   10;
            CacheDelay.ExtendedGroupName        =   extgrp;
            CacheDelay.EnglishName              =   "Cache delay";
            CacheDelay.Description              =   "Delay in minutes for the project items elements";
            bSettings.AddSetting(CacheDelay);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]CalloutCampaignSI InitSettings END.",ModuleTraceSwitch.Sw.Info);
        }
    }

    public partial class CalloutCampaign : PortalCRMModuleControl
    {

		#region Global Implementation

        public override Guid GuidID 
		{
			get
			{
				return new Guid("{F11F9035-A4D4-46f4-BCBA-4BE6DAB51DD4}");
			}
		}

		#endregion Global Implementation
 
        #region Ctor

        CalloutCampaignUtil _Util  =   null;
        CalloutCampaignUtil Util
        {
            get
            {
                if ( _Util == null )
                    _Util                           =   new CalloutCampaignUtil(Wrapper,ResetWrapper,EntityID,Settings[PStr.CacheDelay].ToInt());
                return _Util;
            }
        }

        /// <summary>
		/// Ctor Set the properties of the module
		/// </summary>
        public CalloutCampaign()
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose || CalloutCampaignTrace.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CalloutCampaign Ctor enter at: {0}", DateTime.UtcNow), (ModuleTraceSwitch.Sw.TraceVerbose ) ? ModuleTraceSwitch.Sw.Info :CalloutCampaignTrace.Sw.Info);
        }

        public override string ExtendedResourcesGroup 
		{
            get
            {
       			const string extgrp                 =   "CALLOUTCAMPAIGN";
                return extgrp;
            }
		}

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new CalloutCampaignSI(bSettings,this);
        }

        #endregion Ctor

        protected void Page_Load(object sender, EventArgs e)
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose || CalloutCampaignTrace.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CalloutCampaign Page_Load BEG IsPostBack: {0}, IsInEntityCreationMode : {1}",IsPostBack,IsInEntityCreationMode), (ModuleTraceSwitch.Sw.TraceVerbose ) ? ModuleTraceSwitch.Sw.Info :CalloutCampaignTrace.Sw.Info);
            if ( ! IsPostBack )
            {
                EditTable.DataBind();
            }
        }


        void EditTable_NeedDataSource(object sender,SettingsTableEventArgs e)
        {
			if (CalloutCampaignTrace.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CalloutCampaign EditTable_NeedDataSource BEG TableType: {0}",e.CurrentTable.TableType),CalloutCampaignTrace.Sw.Info);
            e.CurrentTable.DataSource         =   Util.cSettings;
			if (CalloutCampaignTrace.Sw.TraceVerbose)
                Trace2.WriteLine("[V]CalloutCampaign EditTable_NeedDataSource END.",CalloutCampaignTrace.Sw.Info);
        }

        void  CallingCampaign_ADNUpdateModule(object sender, ModuleEventArgs e)
        {
            // Only Update if Input Data is Valid
            if ( CalloutCampaignTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CalloutCampaign CallingCampaign_ADNUpdateModule BEG ModuleID: {0}", ModuleID),CalloutCampaignTrace.Sw.Info);
            // Update settings in the database
            EditTable.UpdateControls();
            if ( CalloutCampaignTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CalloutCampaign CallingCampaign_ADNUpdateModule END ModuleID: {0}", ModuleID),CalloutCampaignTrace.Sw.Info);
        }

        void EditTable_UpdateControl(object sender,SettingsTableItemEventArgs e)
        {
			if (CalloutCampaignTrace.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CalloutCampaign EditTable_UpdateControl BEG TableType: {0}",e.SettingType),CalloutCampaignTrace.Sw.Info);
            string key                          =   e.CurrentItem.EnglishName; // beware don't take editcontrol ID because it is upper case
            string val                          =   e.CurrentItem.Value;
            
            ADNDynCallingCampaignItemValue piv  =   null;
            if ( Util.PValuesList.ContainsKey(key) )
            {
			    if (CalloutCampaignTrace.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CalloutCampaign EditTable_UpdateControl piv found key: {0}",key),CalloutCampaignTrace.Sw.Info);
                piv                             =   Util.PValuesList[key];
                piv.value                       =   val;
                piv.Update();
			    if (CalloutCampaignTrace.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CalloutCampaign EditTable_UpdateControl piv updated val: {0}",val),CalloutCampaignTrace.Sw.Info);
            }
            else
            {
			    if (CalloutCampaignTrace.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CalloutCampaign EditTable_UpdateControl creating piv fo key: {0}",key),CalloutCampaignTrace.Sw.Info);
                piv                             =   new ADNDynCallingCampaignItemValue(Wrapper,key);
                // get the project item with same key
                ADNDynCallingCampaignItem pi    =   Util.PItemLst[key];
                piv.itemid                      =   pi.callingcampaignitemid;
                piv.value                       =   val;
                piv.calloutcampaignid           =   EntityID;
                piv.Create();
			    if (CalloutCampaignTrace.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CalloutCampaign EditTable_UpdateControl piv created val: {0}",val),CalloutCampaignTrace.Sw.Info);
            }
			if (CalloutCampaignTrace.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CalloutCampaign EditTable_UpdateControl END key: {0}",key),CalloutCampaignTrace.Sw.Info);
        }

        #region Std overrides 

        /// <summary>
        /// Triggered from Page.OnPreInit
        /// We must set the Add button before buttons are initialized by base method
        public override void Page_PreInit(object sender, EventArgs e)
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose || CalloutCampaignTrace.Sw.TraceVerbose)
                Trace2.WriteLine("[V]CalloutCampaign Page_PreInit BEG.",(ModuleTraceSwitch.Sw.TraceVerbose ) ? ModuleTraceSwitch.Sw.Info :CalloutCampaignTrace.Sw.Info);
            base.Page_PreInit(sender, e);
            string strcult                  =   Helpers.LocalizeHelper.GetCurrentNeutralCultureName();
            updateBtn.SkinID                =   "Update_"+strcult;
            EditTable.NeedDataSource        +=  new SettingsTableEventHandler(EditTable_NeedDataSource);
 			if (ModuleTraceSwitch.Sw.TraceVerbose || CalloutCampaignTrace.Sw.TraceVerbose)
                Trace2.WriteLine("[V]CalloutCampaign Page_PreInit END.",(ModuleTraceSwitch.Sw.TraceVerbose ) ? ModuleTraceSwitch.Sw.Info :CalloutCampaignTrace.Sw.Info);
       }

		override protected void OnInit(EventArgs e)
		{
			if (ModuleTraceSwitch.Sw.TraceVerbose || CalloutCampaignTrace.Sw.TraceVerbose)
                Trace2.WriteLine("[V]CalloutCampaign OnInit BEG.",(ModuleTraceSwitch.Sw.TraceVerbose ) ? ModuleTraceSwitch.Sw.Info :CalloutCampaignTrace.Sw.Info);
			InitializeComponent();
			base.OnInit(e);
			if (ModuleTraceSwitch.Sw.TraceVerbose || CalloutCampaignTrace.Sw.TraceVerbose)
                Trace2.WriteLine("[V]CalloutCampaign OnInit END.",(ModuleTraceSwitch.Sw.TraceVerbose ) ? ModuleTraceSwitch.Sw.Info :CalloutCampaignTrace.Sw.Info);
		}

		private void InitializeComponent()
		{
            Load                            +=  new EventHandler(Page_Load);
            if ( !IsInEntityCreationMode )
            {
                EditTable.UpdateControl     +=  new UpdateControlEventHandler(EditTable_UpdateControl);
                ADNUpdateModule             +=  new PortalModuleEventHandler(CallingCampaign_ADNUpdateModule);
            }
 		}


        #endregion
    }
}